package com.tan.controller;

import com.tan.entity.Conditions;
import com.tan.entity.InfoEntity;
import com.tan.entity.ResultInfo;
import com.tan.service.Dom4jService;
import com.tan.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author 谭永超 [www.tanyongchao.tk:8090/SumDome/index]
 * @Date 2018/8/21 11:12
 * @Description
 */
@Controller
@RequestMapping(value = "/select")
public class selectController {
    @Autowired
    private Dom4jService dom4jService;

    /**
     *  查询出所有的航班信息
     * @param model  存入查询出来的变量
     * @return java.lang.String 返回对应的页面
     *
     */
    @RequestMapping(value = "/all",method = {RequestMethod.GET})
    public String toSelect(Model model) throws Exception {
        List<InfoEntity> list =  dom4jService.allInteger();
        model.addAttribute("list",list);
        model.addAttribute("size",list.size());
        return  "haha";
    }
    /**    
     *  
     *
     * @param bindingResult 表单验证
     * @return com.tan.entity.ResultInfo<java.util.List<com.tan.entity.InfoEntity>>
     *       
     */ 
    @RequestMapping(value = "/condition",method = {RequestMethod.POST})
    @ResponseBody
    public ResultInfo<List<InfoEntity>> doSelect(@Valid @RequestBody Conditions conditions, BindingResult bindingResult){
        ResultUtils<List<InfoEntity>>  resultUtils = new ResultUtils<List<InfoEntity>>();
        if(bindingResult.hasErrors()){
            return resultUtils.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        return resultUtils.success(dom4jService.selectByPlaceAndDate(conditions.getDepartureAirportName(),
                conditions.getArrivalAirportName(),
                conditions.getDate()));
    }
}
